#include "CCustomBadge.h"
#include <QPainter>
#include <QApplication>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <QTimer>

#define ICON
#define OFFSET ( float(1) / float(3) )

CCustomBadge::CCustomBadge(QWidget *p) :
    QWidget(p),
    parent_(p)
{
    if (p == Q_NULLPTR)
    {
        parent_ = QApplication::desktop();
    }
    setParent(parent_);
    setMouseTracking(true);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_AlwaysStackOnTop);
    setCursor(Qt::OpenHandCursor);
}

CCustomBadge::~CCustomBadge()
{

}

void CCustomBadge::paintEvent(QPaintEvent* e)
{
    QPainter p(this);
    p.setRenderHint(QPainter::SmoothPixmapTransform);
    p.drawImage(rect(), icon_);
    // p.fillRect(rect(), Qt::black);

    return QWidget::paintEvent(e);
}

void CCustomBadge::mousePressEvent(QMouseEvent *e)
{
    flag_drag_ = true;
    flag_drag_continue_ = false;
    drag_posx_ = e->globalPos().x();
    drag_posy_ = e->globalPos().y();

    ms_ts_ = e->timestamp();
    return QWidget::mousePressEvent(e);
}

void CCustomBadge::mouseMoveEvent(QMouseEvent *e)
{
    if (!flag_drag_)
    {
        return;
    }

    int xdst = rect().width() * OFFSET;
    int ydst = rect().height() * OFFSET;
    QPoint p = e->globalPos();

    int offsetx = p.x() - drag_posx_;
    int offsety = p.y() - drag_posy_;
    if (!flag_drag_continue_ && (qAbs(offsetx) < 10 || qAbs(offsety) < 10))
    {
        return;
    }

    auto g1 = geometry();
    auto g2 = parent_->rect();
    if (g1.topLeft().x() + offsetx - g2.topLeft().x() < (-xdst))
    {
        offsetx = offsetx < 0 ? 0 : offsetx;
    }
    if (g1.bottomRight().x() + offsetx - g2.bottomRight().x() > xdst)
    {
        offsetx = offsetx > 0 ? 0 : offsetx;
    }
    if (g1.topLeft().y() + offsety - g2.topLeft().y() < (-ydst))
    {
        offsety = offsety < 0 ? 0 : offsety;
    }
    if (g1.bottomRight().y() + offsety - g2.bottomRight().y() > ydst)
    {
        offsety = offsety > 0 ? 0 : offsety;
    }

    QPoint tp1(geometry().topLeft().x() + offsetx, geometry().topLeft().y() + offsety);
    move(tp1);
    setCursor(Qt::ClosedHandCursor);

//    qDebug()<<"tp1"<<tp1;

    flag_drag_continue_ = true;
    drag_posx_ = p.x();
    drag_posy_ = p.y();
}

void CCustomBadge::mouseReleaseEvent(QMouseEvent *e)
{
    flag_drag_ = false;
    setCursor(Qt::OpenHandCursor);

    auto ts = e->timestamp();
    if (ts - ms_ts_ < 200)
    {
        emit clicked();
    }

    QTimer::singleShot(100, [&]() { automatic_adsorption(); });

    return QWidget::mouseReleaseEvent(e);
}

void CCustomBadge::leaveEvent(QEvent *e)
{
    flag_drag_ = false;
    return QWidget::leaveEvent(e);
}

void CCustomBadge::automatic_adsorption()
{
    if (parent_ == Q_NULLPTR) return;

    int x1 = geometry().topLeft().x();
    int y1 = geometry().topLeft().y();
    int x2 = geometry().bottomRight().x();
    int y2 = geometry().bottomRight().y();
    int dx1 = parent_->rect().topLeft().x();
    int dy1 = parent_->rect().topLeft().y();
    int dx2 = parent_->rect().bottomRight().x();
    int dy2 = parent_->rect().bottomRight().y();
    int dst1 = qAbs(x1 - dx1);
    int dst2 = qAbs(dx2 - x2);
    int dst3 = qAbs(y1 - dy1);
    int dst4 = qAbs(dy2 - y2);

    QPoint newpos(geometry().topLeft());
    if (qMin(dst1, dst2) < qMin(dst3, dst4))
    {
        if (dst1 > dst2)
        {
            // move to right
            newpos.setX(dx2 - rect().width() + (rect().width() * OFFSET));
        }
        else
        {
            // move to left
            newpos.setX(dx1 - (rect().width() * OFFSET));
        }
    }
    else
    {
        if (dst3 > dst4)
        {
            // move to bottom
            newpos.setY(dy2 - rect().height() + (rect().width() * OFFSET));
        }
        else
        {
            // move to top
            newpos.setY(dy1 - (rect().height() * OFFSET));
        }
    }
    move(newpos);
}
